const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabItemActive: 0,//头部tab默认选中
    btype: 3, // 默认3是全部
    tabItemList: [//tab切换
      { name: '全部', id: 3, infotype: '' },
      { name: '拍卖', id: 1, infotype: 1 },
      { name: '招标', id: 1, infotype: 2 },
      { name: '自营标的', id: 2, infotype: '' },
    ],
    recommend: '最新发布',
    filter: 2,//最新发布
    recommendList: [
      // { name: '综合推荐', id: 1 },
      // { name: '最新发布', id: 2 }
    ],

    recommendStatus: "none",
    typecn: '物资分类',
    typeName: '',
    addrcn: '所在地区',
    addrcnText: '',
    areacodes: '',
    filtercn: '筛选',
    releasetime: 0,//发布时间，0不限
    filterTime: [//发布时间筛选列表
      { name: '今天发布', id: '1' },
      { name: '一周内发布', id: '2' },
      { name: '一个月内发布', id: '3' }
    ],
    bmtype: 0,//报名方式，0不限
    registrationMethod: [//报名方式
      { name: '网络报名', id: 1 },
      { name: '现场报名', id: 2 }
    ],
    pmtype: 0,//拍卖方式，
    pmtypeList: [//拍卖方式
      { name: '现场拍卖', id: 3 },
      { name: '网络拍卖', id: 2 },
      { name: '其他', id: 1 }
    ],
    pmstate: 0,//拍卖状态，0不限
    auctionStatus: [//拍卖状态
      { name: '即将开拍', id: 1 },
      { name: '正在进行', id: 2 },
      { name: '已结束', id: 3 }
    ],
    prostatus: 0,//拍卖时间，0不限
    auctionTime: [//拍卖时间
      { name: '1天内', id: 1 },
      { name: '3天内', id: 2 },
      { name: '5天内', id: 3 },
      { name: '7天内', id: 4 },
      { name: '15天内', id: 5 },
      { name: '30天内', id: 6 }
    ],
    typeRecord: {
      categoryListIndex: '',
      categoryListTowIndex: '',
      categorycode: ''
    },
    addrRecord: {
      provinceind: '',
      cityind: '',
      areacodes: ''
    },
    filterRecord: {
      releasetime: '',
      bmtype: '',
      pmstate: '',
      prostatus: '',
    },
    // bmTypecn: '报名方式',
    // timecn: '时间不限',
    timeStatus: 'none',
    bmStatus: 'none',
    typeStatus: 'none',
    addrStatus: 'none',
    filterStatus: 'none',
    fullStatus: 'none',
    fullStatus2: 'none',
    iconTime: ' icon-arrow-down-bold',
    iconBmType: ' icon-arrow-down-bold',
    iconType: ' icon-arrow-down-bold',
    iconAddr: ' icon-arrow-down-bold',
    iconRecommend: ' icon-arrow-down-bold',
    iconFilter: ' icon-shaixuan',
    datatype: 2,
    category: '',
    categorycode: '',
    provinceind: '',
    cityind: '',
    province: '',
    prostatus: 0,
    pageNo: 1,
    city: '',
    isLike: 1,
    pageSize: 10,
    keyWord: '',
    // k: '',
    address: [],
    addressList: [],
    categoryList: [],
    categoryListIndex: '',
    categoryListTow: [],
    categoryListTowIndex: '',
    product: [],
    // timelist: [{ name: '时间不限', num: 0 }, { name: '1天内', num: 1 }, { name: '3天内', num: 2 }, { name: '5天内', num: 3 }, { name: '7天内', num: 4 }, { name: '15天内', num: 5 }],
    // bmTypelist: [{ name: '不限', num: 0 }, { name: '网络报名', num: 1 }, { name: '现场报名', num: 2 }],
    // bmTypeListIndex: 0,
    // timelistIndex: 0,
    hasMore: true,
    noMore: false,
    searchDel: false,
    FjLoginLog: wx.getStorageSync('FjLoginLog'),
    oftenList: wx.getStorageSync(wx.getStorageSync('FjLoginLog').UID + '') || [],//最近常看
    showPMtime: true, // 是否显示排名时间
    showBack: false, // 是否显示置顶按钮
    scrollViewHeight: 0, // 列表高度
    triggered: false,
    mapKey: 'e24353dac4e7753f2eea4ce5e3112b2d',
    titleDidColor: false, // 是否显示点击以后的颜色(列表点击以后就会变成灰色)
  },
  // 搜索
  zcczSearch: function (e) {
    this.setData({
      k: app.trim(e.detail.value)
    })

    if (this.data.k.length > 0) {
      this.setData({
        searchDel: true
      })
    } else {
      this.setData({
        searchDel: false
      })
    }
  },

  // 头部tab切换
  changeTabs: function (e) {
    this.setData({
      pageNo: 1,
      product: [],
      tabItemActive: e.currentTarget.dataset.index,
      btype: e.currentTarget.dataset.info.id,
      infotype: e.currentTarget.dataset.info.infotype
    })
    this.getAuctionList();
  },

  // 点击综合推荐
  changeRecommend: function () {
    /*
    recommendStatus : 综合推荐
    typeStatus: 物资分类
    addrStatus: 地区
    filterStatus: 筛选
    */
    if (this.data.recommendStatus == 'none') {
      this.setData({
        typeStatus: 'none',
        addrStatus: 'none',
        filterStatus: 'none',
        iconType: ' icon-arrow-down-bold',
        iconAddr: ' icon-arrow-down-bold',
        recommendStatus: 'block',
        fullStatus: 'block',
        iconRecommend: ' icon-arrow-up-bold'
      })
    } else {
      this.setData({
        recommendStatus: 'none',
        fullStatus: 'none',
        iconRecommend: ' icon-arrow-down-bold'
      })
    }
  },
  // 切换综合推荐列表
  changeRecommendActive: function (e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      pageNo: 1,
      // product: [],
      recommend: this.data.recommendList[index].name,
      filter: this.data.recommendList[index].id,
      recommendStatus: 'none',
      fullStatus: 'none',
      iconRecommend: ' icon-arrow-down-bold'
    })
    // this.getZcList()
    this.getAuctionList();
  },

  // 地区选择确定
  addrConfirm: function () {
    this.setData({
      pageNo: 1,
      product: [],
      addrcn: this.data.addrcnText.slice(0, 4),
      // 地区回显
      "addrRecord.provinceind": this.data.provinceind,
      "addrRecord.cityind": this.data.cityind,
      "addrRecord.areacodes": this.data.areacodes,
    })
    this.changeAddr();
    // this.getZcList();
    this.getAuctionList();
  },
  // 地区重置
  addrReset: function () {
    this.setData({
      addrcnText: '所在地区',
      cityind: '',
      provinceind: '',
      province: '',
      city: '',
      areacodes: '',
      pageNo: 1,
      addrcn: '所在地区'
    })
    // this.changeAddr();
    // this.getZcList()
  },


  // 点击当前位置
  chooseLocate: function () {
    console.log('addrcn', this.data.addrcn)
    this.data.addressList.forEach((item, index) => {
      // provName: result.data.result.ad_info.province,
      // cityName: 
      if (this.data.provName == item.areaName) {
        this.setData({
          provinceind: index,
          // p1: '0' + item.pcid,
        })
      }
    }) 
    
    // console.log('看看有吗----', this.data.addressList[this.data.provinceind].children)
    this.data.addressList[this.data.provinceind].children.forEach((item, index) => {
      if (this.data.cityName == item.areaName) {
        this.setData({
          addrcnText: item.areaName.slice(0, 4),
          cityind: index,
          areacodes: item.areaCode,
          addrcn: item.areaName.slice(0, 4),
          // p1: item.pcid,
          // p: item.pcid
        })
      }
    })
    // console.log(this.data.p)
  },
  // 定位
  getAddr: function () {
    var that = this
    wx.getSetting({
      success(res) {
        // 如果已拒绝授权，则打开设置页面
        if (res.authSetting['scope.userLocation'] == false) {
          that.setData({
            noOpenLocate: true
          })
          wx.openSetting({
            success(res) {
              console.log('settingres==', res)
              // 如果授权位置, 重新获取当前位置
              if (res.authSetting['scope.userLocation'] == true) {
                that.setData({
                  noOpenLocate: false
                })
                that.getMyLocation()
              } else {
                wx.showToast({
                  title: '授权失败',
                  icon: 'error'
                })
              }

            }
          })
        } else {
          that.getMyLocation()
        }
      }
    })

  },
  getMyLocation: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        that.getweizhi(latitude, longitude)
      }
    })
  },
  // 获取位置
  getweizhi: function (latitude, longitude) {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.request({
      url: `https://restapi.amap.com/v3/geocode/regeo?output=JSON&location=${longitude},${latitude}&key=e24353dac4e7753f2eea4ce5e3112b2d`,
      header: {
        'Content-Type': 'application/json'
      },
      success: function (result) {
        wx.hideLoading()
        console.log('gao-de-result==', JSON.stringify(result))
        that.setData({
          provName: result.data.regeocode.addressComponent.province,
          cityName: result.data.regeocode.addressComponent.city,
        })
      },
      fail: function (res) {
        app.tishi(res)
        // that.getFJ_Get_RecListKey();
      }
    })
    // wx.request({
    //   url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${that.data.mapKey}`,
    //   header: {
    //     'Content-Type': 'application/json'
    //   },
    //   success: function (result) {
    //     wx.hideLoading()
    //     console.log('result==', JSON.stringify(result))
    //     that.setData({
    //       provName: result.data.result.ad_info.province,
    //       cityName: result.data.result.ad_info.city,
    //     })
    //   },
    //   fail: function (res) {
    //     app.tishi(res)
    //     // that.getFJ_Get_RecListKey();
    //   }
    // })
  },
  // 重新定位
  againLocation: function () {
    this.getAddr()
  },

  // 点击筛选
  changeFilter: function () {
    if (this.data.filterStatus == 'none') {
      this.setData({
        typeStatus: 'none',
        addrStatus: 'none',
        recommendStatus: 'none',
        iconType: ' icon-arrow-down-bold',
        iconAddr: ' icon-arrow-down-bold',
        iconRecommend: ' icon-arrow-down-bold',
        filterStatus: 'block',
        fullStatus: 'none',
        fullStatus2: 'block',
      })
    } else {
      this.setData({
        filterStatus: 'none',
        fullStatus2: 'none',
        iconType: ' icon-arrow-down-bold'
      })
    }
  },

  // 选择发布时间
  chooseTime: function (e) {
    if (this.data.releasetime == e.currentTarget.dataset.info.id) {
      this.setData({
        releasetime: '',
      })
    } else {
      this.setData({
        releasetime: e.currentTarget.dataset.info.id,
      })
    }
  },

  // 选择报名方式
  chooseState: function (e) {
    console.log('e==', e)
    if (this.data.bmtype == e.currentTarget.dataset.info.id) {
      this.setData({
        bmtype: ""
      })
    } else {
      this.setData({
        bmtype: e.currentTarget.dataset.info.id
      })
    }
  },

  // 选择拍卖状态
  chooseAuctionState: function (e) {
    let ids = e.currentTarget.dataset.info.id
    if (this.data.pmstate == ids) {
      this.setData({
        pmstate: '',
        showPMtime: true,
        prostatus: ''
      })
    } else {
      this.setData({
        pmstate: ids,
        prostatus: '',
        showPMtime: ids == 1 // 如果ids==1说明是即将开拍，2正在进行，3已结束，只有1才显示排名时间
      })
    }
  },

  // 选择拍卖时间
  chooseAuctionTime: function (e) {
    if (this.data.prostatus == e.currentTarget.dataset.info.id) {
      this.setData({
        prostatus: ''
      })
    } else {
      this.setData({
        prostatus: e.currentTarget.dataset.info.id
      })
    }
  },
// 选择拍卖方式
choosePmtypeState: function (e) {
    if (this.data.prostatus == e.currentTarget.dataset.info.id) {
      this.setData({
        pmtype: ''
      })
    } else {
      this.setData({
        pmtype: e.currentTarget.dataset.info.id
      })
    }
  },
  // 筛选确定
  filterConfirm: function () {
    this.setData({
      pageNo: 1,
      product: []
    })
    this.changeFilter();
    this.getAuctionList();
    if (this.data.releasetime !== '' || this.data.pmtype !== '' || this.data.pmstate !== '' || this.data.prostatus !== '') {
      this.setData({
        filterActive: true
      })
    } else {
      this.setData({
        filterActive: false
      })
    }
    // 筛选回显
    this.setData({
      "filterRecord.releasetime": this.data.releasetime,
      "filterRecord.bmtype": this.data.bmtype,
      "filterRecord.pmstate": this.data.pmstate,
      "filterRecord.prostatus": this.data.prostatus,
      "filterRecord.pmtype": this.data.pmtype,

    })
  },

  // 筛选重置
  filterReset: function () {
    this.setData({
      releasetime: '',
      bmtype: '',
      pmstate: '',
      prostatus: '',
      pmtype:''
    })
  },
  // 新增热搜词
  addHotData: function () {
    var that = this
    var api_input = {
      stype: "10",
      lytype: "2",
      loginuid: wx.getStorageSync('FjLoginLog').UID,
      keyword: this.data.k
    }
    app.getDaoPost('api/FeijiuApp/Feijiu_Add_SearchRecord', api_input, function (r) {
      if (r.code == 200) {
        that.getHotData()
      } else {
      }
    })
  },
  // 获取热搜词
  getHotData: function () {
    var that = this
    app.getDaoPost('api/FeijiuApp/Feijiu_GetGGKeyList_v2024', { loginuid: wx.getStorageSync('FjLoginLog').UID, platform_type: 4 }, function (r) {
      if (r.code == 200) {
        that.setData({
          hotDataList: r.data
        })
      } 
      that.getScrollViewHeight()
    })
  },

  // 点击热搜词
  changeHot: function (e) {
    if (this.data.keyWord == e.currentTarget.dataset.info) {
      this.setData({
        keyWord: '',
        searchDel: false,
        pageNo: 1,
        product: [],
      })
    } else {
      this.setData({
        keyWord: e.currentTarget.dataset.info,
        searchDel: true,
        pageNo: 1,
        product: [],
      })
      // 这个接口是添加埋点记录的
      var api_input = {
        stype: "10",
        lytype: "2",
        loginuid: wx.getStorageSync('FjLoginLog').UID,
        keyword: e.currentTarget.dataset.info
      }
      app.getDaoPost('api/FeijiuApp/Feijiu_Add_SearchRecord', api_input, function (r) { })
    }


    this.getAuctionList();
  },

  searchBtn: function () {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 10
    })
    this.setData({
      pageNo: 1,
      product: [],
      keyWord: this.data.k,
    })
    
    this.addHotData()
    this.getAuctionList();
  },
  bindfocus: function() {
    this.chickFull()
  },
  searchDelF: function () {
    this.setData({
      // k: '',
      keyWord: '', //原来默认值 =》c2VhcmNo
      pageNo: 1,
      product: [],
      searchDel: false
    })
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 10
    })
    this.getAuctionList();
  },
  changeTime: function () {
    if (this.data.timeStatus == 'none') {
      this.setData({
        typeStatus: 'none',
        addrStatus: 'none',
        bmStatus: 'none',
        iconType: ' icon-xiangxia',
        iconAddr: ' icon-xiangxia',
        timeStatus: 'block',
        fullStatus: 'block',
        iconTime: ' icon-xiangshang'
      })
    } else {
      this.setData({
        timeStatus: 'none',
        fullStatus: 'none',
        iconTime: ' icon-xiangxia'
      })
    }
  },
  bmtype: function () {
    if (this.data.bmStatus == 'none') {
      this.setData({
        typeStatus: 'none',
        addrStatus: 'none',
        timeStatus: 'none',
        iconType: ' icon-xiangxia',
        iconAddr: ' icon-xiangxia',
        iconTime: ' icon-xiangxia',
        fullStatus: 'block',
        bmStatus: 'block',
        iconBmType: ' icon-xiangshang'
      })
    } else {
      this.setData({
        fullStatus: 'none',
        bmStatus: 'none',
        iconBmType: ' icon-xiangxia'
      })
    }
  },

  changeAddr: function () {
    if (this.data.addrStatus == 'none') {
      this.setData({
        recommendStatus: 'none',
        typeStatus: 'none',
        bmStatus: 'none',
        timeStatus: 'none',
        iconTime: ' icon-arrow-down-bold',
        iconType: ' icon-arrow-down-bold',
        addrStatus: 'block',
        fullStatus: 'block',
        iconAddr: ' icon-arrow-up-bold',
        iconRecommend: ' icon-arrow-down-bold'
      })
    } else {
      this.setData({
        addrStatus: 'none',
        fullStatus: 'none',
        iconAddr: ' icon-arrow-down-bold'
      })
    }
  },
  // 点击物资分类
  changeType: function () {
    console.log('点击了物资分类')
    if (this.data.typeStatus == 'none') {
      this.setData({
        categoryListIndex: this.data.typeRecord.categoryListIndex,
        categoryListTowIndex: this.data.typeRecord.categoryListTowIndex,
        addrStatus: 'none',
        recommendStatus: 'none',
        filterStatus: 'none',
        iconAddr: ' icon-arrow-down-bold',
        iconTime: ' icon-arrow-down-bold',
        iconRecommend: ' icon-arrow-down-bold',
        typeStatus: 'block',
        fullStatus: 'block',
        iconType: ' icon-arrow-up-bold',
      })
      if (this.data.typeRecord.categoryListIndex) {
        this.CategoryTow(this.data.categoryList[this.data.typeRecord.categoryListIndex].cat_code)

      }
    } else {
      this.setData({
        typeStatus: 'none',
        fullStatus: 'none',
        iconType: ' icon-arrow-down-bold'
      })
    }
  },
  chickFull: function () {
    this.setData({
      timeStatus: 'none',
      typeStatus: 'none',
      addrStatus: 'none',
      recommendStatus: 'none',
      filterStatus: 'none',
      fullStatus: 'none',
      fullStatus2: 'none',
      bmStatus: 'none',
      iconTime: ' icon-arrow-down-bold',
      iconType: ' icon-arrow-down-bold',
      iconAddr: ' icon-arrow-down-bold',
      iconBmType: ' icon-arrow-down-bold',
      // 物资分类回显
      categoryListIndex: this.data.typeRecord.categoryListIndex,
      categoryListTowIndex: this.data.typeRecord.categoryListTowIndex,
      categorycode: this.data.typeRecord.categorycode,
      // 地区回显
      provinceind: this.data.addrRecord.provinceind,
      cityind: this.data.addrRecord.cityind,
      areacodes: this.data.addrRecord.areacodes,
      // 筛选回显
      releasetime: this.data.filterRecord.releasetime,
      bmtype: this.data.filterRecord.bmtype,
      pmstate: this.data.filterRecord.pmstate,
      prostatus: this.data.filterRecord.prostatus,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  toView: function (e) {
    if (e.currentTarget.dataset.btype == 2) { // 自营标的
      let parame = {
        LoginUID : this.data.FjLoginLog.UID,
        auctioncode: e.currentTarget.dataset.auctioncode,
        code : e.currentTarget.dataset.code
      }
      wx.showLoading({
        title: '...',
      })
      app.getDaoPostSearchNew('api/FeijiuApp/Fj_GetJpInfoUrl', parame, (item)=>{
          if (item.code == 200) {
            let urlString = item['data']
            wx.navigateTo({
              url: './zyweb',
              success: (res)=>{
                res.eventChannel.emit('acceptDataFromOpenerPage', { data: urlString })
              }
            })
          }else {
            app.tishi(item.message)
          }
          wx.hideLoading()
      })

    }else {
      wx.navigateTo({
        url: 'detailed?uuid=' + e.currentTarget.dataset.code
      })
    }
    let index = e.currentTarget.dataset.index
    let  arr = this.data.product
    arr[index].showDidTitle = true
    this.setData({
      product: arr
    })
  },
  baoming: function (e) {
    wx.navigateTo({
      url: 'baoming?uuid=' + e.currentTarget.dataset.uuid + '&name=' + e.currentTarget.dataset.name
    })
  },
  // 获取高度
  getScrollViewHeight() {
    let sysinfo = wx.getWindowInfo()
    let topHeight = this.data.hotDataList.length > 0 ? 352 : 272
    let tabbar = 0//sysinfo.screenHeight - sysinfo.safeArea.bottom
    let safeH = (sysinfo.screenHeight - sysinfo.statusBarHeight - 68 - tabbar ) - topHeight / 2
    this.setData({
      scrollViewHeight: safeH,
      topHeight: topHeight
    })
  },


  onLoad: function (options) {
    console.log(options, '招标公告')
    console.log('-------onLoad')
    // if (options) {
    //   this.setData({
    //     typecn: options.pageName,
    //     category: options.GQ_classid
    //   })
    // }

    this.defoltWuZiClass()

    this.setData({
      FjLoginLog: wx.getStorageSync('FjLoginLog'),
      oftenList: wx.getStorageSync(wx.getStorageSync('FjLoginLog').UID + '') || []
    })
    // 获取热搜词
    this.getHotData()
    // 获取当前位置
    this.getAddr()
  
    var that = this;
    var api_input = {};
    // 获取物资分类
    app.getDaoPostJp('api/BulletinService/bulletin_Get_Category_v2024', api_input, function (r) {
      // var fenlei = [{ zid: 0, uid: "", cat_code: "", cat_name: "所有分类", cat_parent_code: "" }];
      var fenlei = [{ zid: 0, uid: "", cat_code: "", cat_name: "不限", cat_parent_code: "" }];
      var categoryList = fenlei.concat(r.data);
      if (r.code == 200) {
        that.setData({
          categoryList: categoryList
        })
      }
    })
    var api_input = {};
    // 获取所在地区
    app.getDaoPostJp('api/BulletinService/bulletin_Get_Area', api_input, function (r) {
      if (r.code == 200) {
        that.setData({
          addressList: r.data
        })
      }
    })


    this.getAuctionList();
    this.getZongHeData()
  },
  // 获取综合推荐数据
  getZongHeData() {
    let api_input = {
      infotype: 2 // 1资产  2拍卖
    }
    var that = this
    app.getDaoPost('api/FeijiuApp/Feijiu_Get_Filter_v2024', api_input, function (r) {
      if (r.code == 200) {
        that.setData({
          recommendList: r.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  proStatus: function (e) {
    if (e.currentTarget.dataset.ind != this.data.timelistIndex) {
      wx.pageScrollTo({
        scrollTop: 0,
        duration: 10
      })
      this.setData({
        timecn: e.currentTarget.dataset.name,
        prostatus: e.currentTarget.dataset.num,
        pageNo: 1,
        product: [],
        timelistIndex: e.currentTarget.dataset.ind
      })
      this.getAuctionList();
      this.chickFull();
    } else {
      this.chickFull();
    }
  },
  pmStatus: function (e) {
    if (e.currentTarget.dataset.ind != this.data.bmTypeListIndex) {
      wx.pageScrollTo({
        scrollTop: 0,
        duration: 10
      })
      this.setData({
        bmTypecn: e.currentTarget.dataset.name == '不限' ? '报名方式' : e.currentTarget.dataset.name,
        bmtype: e.currentTarget.dataset.num,
        pageNo: 1,
        product: [],
        bmTypeListIndex: e.currentTarget.dataset.ind
      })
      this.getAuctionList();
      this.chickFull();
    } else {
      this.chickFull();
    }
  },
  province: function (e) {
    this.setData({
      provinceind: e.currentTarget.dataset.provinceind,
      province: e.currentTarget.dataset.name,
      city: '',
      cityind: '',
      addrcnText: e.currentTarget.dataset.name,
      areacodes: e.currentTarget.dataset.areacode
    })

    // if (e.currentTarget.dataset.provinceind != this.data.provinceind) {
    //   if (e.currentTarget.dataset.provinceind == 0) {
    //     wx.pageScrollTo({
    //       scrollTop: 0,
    //       duration: 10
    //     })
    //     this.setData({
    //       addrcn: '所在地区',
    //       province: '',
    //       pageNo: 1,
    //       product: [],
    //       provinceind: e.currentTarget.dataset.provinceind
    //     })
    //     // this.getAuctionList();
    //     // this.chickFull();
    //   } else {
    //     this.setData({
    //       province: e.currentTarget.dataset.name,
    //       provinceind: e.currentTarget.dataset.provinceind
    //     })
    //   }
    // } else {
    //   // this.setData({
    //   //   cityind:0
    //   // })
    //   this.chickFull();
    // }
  },
  city: function (e) {
    this.setData({
      addrcnText: e.currentTarget.dataset.cityind === 0 ? this.data.provName : e.currentTarget.dataset.name,
      city: e.currentTarget.dataset.name,
      pageNo: 1,
      // product: [],
      cityind: e.currentTarget.dataset.cityind,
      areacodes: e.currentTarget.dataset.areacode
    })
    // console.log(e)
    // if (e.currentTarget.dataset.cityind != this.data.cityind) {
    // if (e.currentTarget.dataset.cityind == 0) {
    //   wx.pageScrollTo({
    //     scrollTop: 0,
    //     duration: 10
    //   })
    //   this.setData({
    //     addrcn: this.data.province,
    //     pageNo: 1,
    //     product: [],
    //     city: '',
    //     cityind: e.currentTarget.dataset.cityind
    //   })
    //   // this.getAuctionList();
    //   // this.chickFull();
    // } else {
    //   wx.pageScrollTo({
    //     scrollTop: 0,
    //     duration: 10
    //   })
    //   this.setData({
    //     addrcn: e.currentTarget.dataset.name,
    //     city: e.currentTarget.dataset.name,
    //     pageNo: 1,
    //     product: [],
    //     cityind: e.currentTarget.dataset.cityind
    //   })
    // this.getAuctionList();
    // this.chickFull();
    // }
    // }else{
    //   this.chickFull();
    // }    
  },
  // 点击物资分类
  category: function (e) {
    // if (e.currentTarget.dataset.ind == 0) { //所有分类
    this.setData({
      pageNo: 1,
      // product: [],
      categoryListTow: [],
      categoryListTowIndex: '',
      typeName: e.currentTarget.dataset.name,
      // category: e.currentTarget.dataset.num,//zid ==0 所有分类
      categoryListIndex: e.currentTarget.dataset.ind,
      categorycode: e.currentTarget.dataset.code
    })
    this.CategoryTow(e.currentTarget.dataset.code)
    // this.getAuctionList();
    // this.chickFull();
    // }
    // else {
    // this.CategoryTow(e.currentTarget.dataset.code)
    // this.setData({
    //   pageNo: 1,
    //   typeName: e.currentTarget.dataset.name,
    //   categoryListIndex: e.currentTarget.dataset.ind
    // })
    // }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // categoryListIndex
    // 自定义tabbar切换
    // if (typeof this.getTabBar === 'function' &&
    //   this.getTabBar()) {
    //   this.getTabBar().setData({
    //     selected: 3
    //   })
    // };
    // 
    this.setData({
      FjLoginLog: wx.getStorageSync('FjLoginLog'),
      oftenList: wx.getStorageSync(wx.getStorageSync('FjLoginLog').UID + '') || []
    })
    // 接收首页自营标的过来的参数
    let zybd = wx.getStorageSync('zybd_key')
    if (zybd) {
      wx.removeStorageSync('zybd_key')
      this.setData({
        pageNo: 1,
        product: [],
        tabItemActive: 3,
        btype: 2,
        infotype: ''
      })
      this.getAuctionList();
      
    }
    // 接收来自详情页标签的数据
    let keyword = wx.getStorageSync('zbxq_key')
    if (keyword) {
      this.setData({
        keyWord: keyword,
        pageNo: 1,
        product: [],
        infotype: '',
        filterActive: false
      })
      // 调用重置
      this.filterReset()
      wx.removeStorageSync('zbxq_key')
      this.getAuctionList();
    }
    // 接收来自详情页推荐更多的数据
    let classDic = wx.getStorageSync('detail_classDic')
    if (classDic.className) {
      this.setData({
        keyWord: '',
        pageNo: 1,
        product: [],
        infotype: '',
        filterActive: false,
        typecn: classDic.className,
        categorycode: classDic.classID,
        typeName: classDic.className,
      })
      setTimeout(() => {
        this.data.categoryList.forEach((item, index) => {
          if (this.data.typecn.indexOf(item.cat_name) !== -1) {
            this.setData({
              "typeRecord.categoryListIndex": index,
            })
          }
        })
      }, 500);

      // 调用重置
      this.filterReset()
      wx.removeStorageSync('detail_classDic')
      this.getAuctionList();
    }
    
  },
  // 一进来物资分类选中为默认
  defoltWuZiClass: function () {
    this.setData({
      keyWord: '',
      pageNo: 1,
      product: [],
      infotype: '',
      filterActive: false,
      typecn: '默认',
      categorycode: '0000',
      typeName: '默认',
      releasetime: 0,
      bmtype: 0,
      pmstate: 0,
      prostatus: 0
    })
    setTimeout(() => {
      this.data.categoryList.forEach((item, index) => {
        if (this.data.typecn.indexOf(item.cat_name) !== -1) {
          this.setData({
            "typeRecord.categoryListIndex": index,
          })
        }
      })
    }, 500);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      product: [],
      pageNo: 1
    })
    this.getAuctionList();
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.noMore) {
      this.getAuctionList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  preventTouchMove: function () {

  },
  // 获取分类 二级
  CategoryTow(code) {
    let that = this
    let api_input = {
      parent_code: code
    }
    app.getDaoPostJp('api/BulletinService/bulletin_Get_Category_v2024', api_input, function (r) {
      if (r.data.length) {
        var fenlei = [{ zid: '', uid: "", cat_code: r.data.cat_code, cat_name: "不限", cat_parent_code: "" }];
        var categoryListTow = fenlei.concat(r.data);
        if (r.code == 200) {
          that.setData({
            categoryListTow: categoryListTow
          })
        }
      }
     
    })
  },
  
  // 点击二级分类
  handlecategoryTow(e) {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 10
    })
    this.setData({
      category: 1,
      pageNo: 1,
      // product: [],
      categoryListTowIndex: e.currentTarget.dataset.ind
    })
    if (e.currentTarget.dataset.name == '不限') {
      this.setData({
        typeName: this.data.categoryList[this.data.categoryListIndex].cat_name,
      })
    } else {
      this.setData({
        typeName: e.currentTarget.dataset.name,
        categorycode: e.currentTarget.dataset.code
      })
    }
    // this.getAuctionList();
    // this.chickFull();
  },
  // 最近常看点击
  typeTagChange: function (e) {
    this.setData({
      // c1: '',
      // indexc1: '',
      // c2: '',
      // indexc2: '',
      typeName: e.currentTarget.dataset.info.name,
      categorycode: e.currentTarget.dataset.info.data
    })
  },
  // 物资分类确定
  typeConfirm: function () {
    if (this.data.FjLoginLog) {
      let res = wx.getStorageSync(wx.getStorageSync('FjLoginLog').UID + '')
      if (res) {
        res = res.filter((item) => {
          return item.name != this.data.typeName
        })
        if (this.data.categorycode) {
          res.unshift({ name: this.data.typeName, data: this.data.categorycode })
        }
        if (res.length > 10) {
          res.splice(10, 1)
        }
        wx.setStorageSync(wx.getStorageSync('FjLoginLog').UID + '', res)
        this.setData({
          oftenList: wx.getStorageSync(wx.getStorageSync('FjLoginLog').UID + ''),
          typecn: this.data.typeName.slice(0, 4)
        })
      } else {
        res = []
        res.unshift({ name: this.data.typeName, data: this.data.categorycode })
        wx.setStorageSync(wx.getStorageSync('FjLoginLog').UID + '', res)
        this.setData({
          oftenList: wx.getStorageSync(wx.getStorageSync('FjLoginLog').UID + ''),
          typecn: this.data.typeName.slice(0, 4)
        })
      }
    }
    this.setData({
      pageNo: 1,
      product: [],
      "typeRecord.categoryListIndex": this.data.categoryListIndex,
      "typeRecord.categoryListTowIndex": this.data.categoryListTowIndex,
      "typeRecord.categorycode": this.data.categorycode,
      typecn: this.data.typeName.slice(0, 4)
    })
    this.getAuctionList();
    this.chickFull();
  },
  // 物资分类重置
  typeReset: function () {
    this.setData({
      // typecn: '',
      category: '',
      typeName: '物资分类',
      categoryListIndex: '',
      categoryListTowIndex: '',
      categorycode: ''
    })
    // this.changeType();
    // this.getZcList();
  },

  getAuctionList: function () {
    var that = this;
    wx.showToast({
      title: "加载中",
      icon: "loading",
    })
    // if (this.data.category == 0) { //所有分类
    //   var categoryname = '';
    // } else {
    //   var categoryname = this.data.typeName
    // }
    let loginUID = ''
    if (app.checkFjLogin()) { //token先看是不是登录了
      loginUID = this.data.FjLoginLog.UID
    }
    var jp_api_input = {
      category: this.data.typeName,
      // province: this.data.province,
      // prostatus: this.data.prostatus,
      pageNo: this.data.pageNo,//当前页
      // city: this.data.city,
      // isLike: this.data.isLike,
      // keyWord: this.data.keyWord,
      pageSize: this.data.pageSize,//每页数量
      // bmtype: this.data.bmTypeListIndex,
      filter: this.data.filter,//最新发布
      keyWord: this.data.keyWord,//关键词
      btype: this.data.btype,//1.招标拍卖，2.自营标的，3.全部
      infotype: this.data.infotype,//1.拍卖，2.招标
      categorycode: this.data.categorycode,//分类编号（单选）
      // pcodes: '',//省份编号（单选）
      // ccodes: '',//城市编号（单选）
      areacodes: this.data.areacodes,
      releasetime: this.data.releasetime,//发布时间
      bmtype: this.data.bmtype,//报名方式
      pmstate: this.data.pmstate,//拍卖状态
      pmtype: this.data.pmtype,//拍卖方式
      prostatus: this.data.prostatus,//拍卖时间
      loginuid : loginUID,
      sortFlag: 2,
    };
    //
    app.getDaoPostSearchNew('api/FeijiuApp/Post_FJ_GetBulletinInfoSearchList_v2024', jp_api_input, function (r) {
      // console.log(r)
      if (r.code == 200) {
        // 当前时间
        const now = new Date().getTime();
        wx.hideToast()
        if (that.data.pageNo == 1) {
          that.setData({
            product: [],
          })
        }
        if (r.data.length > 0) {
          for (var i = 0; i < r.data.length; i++) {
            // var ss = r.data[i].endtime.substr(6,10);
            r.data[i].starting_price = r.data[i].starting_price ? r.data[i].starting_price : '暂无'
            r.data[i].timestate = 'other'
            r.data[i].photo = r.data[i].photo.split('|')[0]
             if(r.data[i].keyword.length) {
                let keywordArr = r.data[i].keyword.split(',')
                r.data[i].keyword = keywordArr
             } else {
                r.data[i].keyword = ''
             }
            
            if (r.data[i].time == "/Date(-28800000)/") {
              r.data[i].time = '待定';
              r.data[i].endtime = '待定';
              r.data[i].distance = '待定'
              r.data[i].releasetime = app.timeConvert(r.data[i].releasetime.substr(6, 10), 1);
            } else {
              r.data[i].time = app.timeConvert(r.data[i].time.substr(6, 10), 1);
              r.data[i].releasetime = app.timeConvert(r.data[i].releasetime.substr(6, 10), 1);
              // 倒计时 (当前时间 - 后台时间)
              var newDateStr = r.data[i].time.replace(/-/g, '/');
              let distance = now - new Date(newDateStr).getTime() 
              let hours =  distance / (60 * 60 * 1000) | 0
              if (hours >= 24) {
               // 当前时间 > 后台返回的时间，并且都超过了24小时，那肯定已经结束了
               r.data[i].distance = '已结束'
               r.data[i].timestate = 'yjs'
              }else if (hours >= 0 && hours < 24) {
                // 当前时间 > 后台返回的时间，但是没有超过24小时,状态就代表进行中
                r.data[i].distance = '正在进行'
                r.data[i].timestate = 'jxz'
              }else if (hours >= -24 && hours < 0) {
                // 当前时间 < 后台返回的时间，并且在24小时之内就要到达后台返回的时间
                // 即将开拍
                r.data[i].distance = '即将开拍'
                r.data[i].timestate = 'jjkp'
              }else if (hours < -24)  {
                // 局开拍xxx天
                let days = Math.abs((hours / 24) | 0)
                r.data[i].distance = days + '天'
                r.data[i].timestate = 'jkp'
              }
     
            }

            if (app.getCurrentDate() < r.data[i].endtime || r.data[i].endtime == '' || r.data[i].endtime == '待定') {
              r.data[i].bmstatus = 1;
            } else {
              r.data[i].bmstatus = 0;
            }
          }

          // var list1 = that.data.product.concat(r.data);
          var list1 = that.data.product.concat(r.data);
          that.setData({
            product: list1,
            pageNo: that.data.pageNo + 1
          })

          if (list1.length < 10) {
            that.setData({
              hasMore: false,
              noMore: true,
            })
          }else{
            that.setData({
              hasMore: true,
              noMore: false,
            })
          }
        } else {
          that.setData({
            hasMore: false,
            noMore: true,
          })
        }
      }
    })
  },
   // 点击返回顶部
   backTopClick: function() {
    // wx.pageScrollTo({
    //   scrollTop: 0
    // })
    this.setData({
      scrollTopY: 0
    })
  },
  // 下拉刷新
  bindrefresherrefresh(e) {
    this.setData({
      triggered: true
    })
    this.setData({
      // product: [],
      pageNo: 1
    })
    this.getAuctionList();
    setTimeout(() => {
      this.setData({
        triggered: false
      })
    }, 1000);
    
  },
  // 底部
  bindscrolltolower(e){
    console.log('到底部了')
    
    if (!this.data.noMore) {
      this.getAuctionList();
    }
    // if (!this.data.noMore) {
      
    // }
  },
  // 滑动事件
  binddragging(e) {
    
    if (e.detail.scrollTop >= 1000) {
      this.setData({
        showBack: true
      })
    }else {
      this.setData({
        showBack: false
      })
    }
  },
  // 滚动到顶部
  bindscrolltoupper(e) {
    this.setData({
      showBack: false
    })
  }

})